package com.jiyun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiyun.mapper.chengyuanMapper;
import com.jiyun.mapper.juminMapper;
import com.jiyun.model.chengyuan.Chengyuan;
import com.jiyun.model.jumin.Jumin;
import com.jiyun.result.Result;
import com.jiyun.result.ResultCodeEnum;
import com.jiyun.service.juminService;
import com.jiyun.vo.jumin.JuminVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class juminServiceImpl extends ServiceImpl<juminMapper, Jumin> implements juminService {
    @Autowired
    private juminMapper juminMapper;
    @Autowired
    private chengyuanMapper chengyuanMapper;

    @Override
    public Result juminFndAll(Jumin jumin,Integer pn,Integer ps) {
        Page<Jumin> page = new Page<>(pn, ps);
        List<Jumin> list = juminMapper.juminFindAll(jumin, page);
        page.setRecords(list);
        page.setTotal(page.getTotal());
        return Result.build(page, ResultCodeEnum.SUCCESS);
    }

    @Override
    public void juminAdd(Jumin jumin,String chengyuans) {
        if (jumin.getId()==null){
            jumin.setStatus(true);
            juminMapper.insert(jumin);
            QueryWrapper<Jumin> w = new QueryWrapper<>();
            w.eq("name",jumin.getName());
            Jumin j = juminMapper.selectOne(w);
            if (!StringUtils.isNotBlank(chengyuans))return;
            String[] split = chengyuans.split(";");
            if (split.length>0){
                for (String s : split) {
                    String[] split1 = s.split(",");
                    Chengyuan c = new Chengyuan();
                    c.setOneid(j.getId());
                    c.setTwoid(Integer.parseInt(split1[0]));
                    c.setGuanxi(split1[1]);
                    chengyuanMapper.insert(c);
                }
            }
        }else {
            juminMapper.updateById(jumin);
            QueryWrapper<Jumin> wrapper = new QueryWrapper<>();
            wrapper.eq("name",jumin.getName());
            Jumin j = juminMapper.selectOne(wrapper);
            if (!StringUtils.isNotBlank(chengyuans))return;
            String[] split = chengyuans.split(";");
            if (split.length>0){
                for (String s : split) {
                    String[] split1 = s.split(",");
                    Chengyuan c = new Chengyuan();
                    c.setOneid(j.getId());
                    c.setTwoid(Integer.parseInt(split1[0]));
                    c.setGuanxi(split1[1]);
                    chengyuanMapper.insert(c);
                }
            }
        }
    }

    @Override
    public Result findByid(Integer id) {
            Jumin jumin = juminMapper.selectById(id);
            return Result.build(jumin,ResultCodeEnum.SUCCESS);
        }

    @Override
    public List<JuminVO> juminOut(Integer[] ids) {
        ArrayList<JuminVO> juminVOS = new ArrayList<>();
        for (Integer id : ids) {
            Jumin jumin = juminMapper.selectById(id);
            JuminVO juminVO = new JuminVO();
            juminVO.setId(jumin.getId());
            juminVO.setName(jumin.getName());
            juminVO.setSex(jumin.getSex());
            juminVO.setShenfenId(jumin.getShenfenId());
            juminVO.setPhone(jumin.getPhone());
            juminVO.setAddress(jumin.getAddress());
            juminVO.setPassword(jumin.getPassword());
            juminVO.setJiguan(jumin.getJiguan());
            juminVO.setMinzu(jumin.getMinzu());
            juminVOS.add(juminVO);
        }
        return juminVOS;
    }

    @Override
    public void de(Integer id) {
        QueryWrapper<Chengyuan> w = new QueryWrapper<>();
        w.eq("oneid",id);
        chengyuanMapper.delete(w);
        QueryWrapper<Chengyuan> w1 = new QueryWrapper<>();
        w.eq("twoid",id);
        chengyuanMapper.delete(w1);
        juminMapper.deleteById(id);
    }

    @Override
    public void upStatus(Integer id) {
        Jumin jumin = juminMapper.selectById(id);
        if (jumin.getStatus()){
            jumin.setStatus(false);
        }else {
            jumin.setStatus(true);
        }
        juminMapper.updateById(jumin);

    }

    @Override
    public List<Jumin> findAll() {
        return juminMapper.selectList(null);
    }
}


